package com.ruoyi.system.controller;

import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.annotation.JsonView;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.system.domain.SysDept;
import com.ruoyi.system.service.ISysDeptService;

/**
 * 部门 提供者
 *
 * @author zmr
 * @date 2019-05-20
 */
@RestController
@RequestMapping("dept")
public class SysDeptController extends BaseController
{
    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 查询部门
     */
    @GetMapping("get/{deptId}")
    public SysDept get(@PathVariable("deptId") Long deptId)
    {
        return sysDeptService.selectDeptById(deptId);
    }

    /**
     * 查询部门(批量)
     */
    @GetMapping("getlist/{deptIdlist}")
    public R getlist(@PathVariable("deptIdlist") String deptIdlist)
    {
        return jacResult(sysDeptService.selectDeptByIdlist(deptIdlist));
    }

    /**
     * 查询部门列表
     */
    @GetMapping("list")
    public R list(SysDept sysDept)
    {
        startPage();
        return result(sysDeptService.selectDeptList(sysDept));
    }

    /**
     * 查询所有可用部门（用户管理前台）
     */
    @GetMapping("list/enable")
    public R listEnable(SysDept sysDept)
    {
        sysDept.setStatus("0");
        return result(sysDeptService.selectDeptList(sysDept));
    }

//    /**
//     * 查询所有可用部门build后台递归（srm业务前台）
//     */
//    @GetMapping("listBuild/enable")
//    @JsonView(value = SysDept.deptBuildView.class)
//    public R listEnableBuild(SysDept sysDept)
//    {
//        sysDept.setStatus("0");
//        return jacResult(sysDeptService.selectDeptListBuild(sysDept));
//    }

    /**
     * 查询所有可用部门build后台递归（srm业务前台）
     */
    @GetMapping("listBuild/enable")
    @JsonView(value = SysDept.deptBuildView.class)
    public R listEnableBuild(String username)
    {
        return jacResult(sysDeptService.selectDeptListBuildEmail(username));
    }


    /**
     * 部门树 2020/10/30 第一次请求
     */
    @GetMapping("getdepttree")
    @JsonView(value = SysDept.deptBuildView.class)
    public R getdepttreebyid(Long deptid,String childrens,String param)
    {
//        List<SysDept> sysDepts = new ArrayList<>();
        if(deptid==null && childrens==null && param==null){
            deptid=0l;
            childrens="1";
            return jacResult(sysDeptService.selectDeptsByParentdeptid(deptid, childrens));
        }else {
          if(param==null){
              return jacResult(sysDeptService.selectDeptsByParentdeptid(deptid, childrens));
          }
            List<SysDept> deptList = sysDeptService.selectDeptListBuildNew(param);
            if(deptList==null){
                return R.error("此人员不存在！");
            }
            return jacResult(deptList);
        }

    }

    /**
     * 部门树 逐级请求
     */
    @GetMapping("getdepttree/{deptid}/{childrens}")
    public R getdepttreebyidfirst(@PathVariable("deptid") Long deptid,@PathVariable("childrens") String childrens)
    {
//        List<SysDept> sysDepts = new ArrayList<>();
        if(deptid==null && childrens==null){
            deptid=0l;
            childrens="1";
        }
        List<Map<String, Object>> maps = sysDeptService.selectDeptsByParentdeptid(deptid, childrens);
        return jacResult(maps);
    }

    /**
     * 审核部门树 逐级请求
     */
    @GetMapping("getverifydepttree")
    public R getverifydepttreebyidfirst(Long deptid,String childrens,String param,boolean ifemail)
    {

        if(deptid==null && childrens==null && param==null){
            deptid=0l;
            childrens="1";
            return jacResult(sysDeptService.selectVerifyDeptsByParentdeptid(deptid, childrens,ifemail));
        }
//        else if(deptid==null){
//            return R.error(505,"请传入部门ID");
//        }
        else {
            if(param==null){
                if(deptid.equals(422l)){
                    return jacResult(sysDeptService.selectVerifyDeptsByParentdeptid(null, "2",ifemail));
                }
                return jacResult(sysDeptService.selectVerifyDeptsByParentdeptid(deptid, childrens,ifemail));
            }
            List<SysDept> deptList = sysDeptService.selectDeptListBuildVerify(param,ifemail);
            if(deptList==null){
                return R.error("此人员不存在！");
            }
            return jacResult(deptList);
        }
    }

//    /**
//     * 部门树 2020/11/2 搜索
//     */
//    @GetMapping("getdepttree/{deptid}/{childrens}")
//    public R getdepttreebyidfirst(@PathVariable("deptid") Long deptid,@PathVariable("childrens") String childrens)
//    {
////        List<SysDept> sysDepts = new ArrayList<>();
//        if(deptid==null && childrens==null){
//            deptid=0l;
//            childrens="1";
//        }
//        List<Map<String, Object>> maps = sysDeptService.selectDeptsByParentdeptid(deptid, childrens);
//        return jacResult(maps);
//    }

    /**
     * 新增保存部门
     */
    @PostMapping("save")
    public R addSave(@RequestBody SysDept sysDept)
    {
        return toAjax(sysDeptService.insertDept(sysDept));
    }

    /**
     * 修改保存部门
     */
    @PostMapping("update")
    public R editSave(@RequestBody SysDept sysDept)
    {
        return toAjax(sysDeptService.updateDept(sysDept));
    }

    /**
     * 删除部门
     */
    @PostMapping("remove/{deptId}")
    public R remove(@PathVariable("deptId") Long deptId)
    {
        return toAjax(sysDeptService.deleteDeptById(deptId));
    }

    /**
     * 加载角色部门（数据权限）列表树
     */
    @GetMapping("/role/{roleId}")
    public Set<String> deptTreeData(@PathVariable("roleId") Long roleId)
    {
        if (null == roleId || roleId <= 0) return null;
        return sysDeptService.roleDeptIds(roleId);
    }
}
